package com.pro_one.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.pro_one.dao.PermissionDao;
import com.pro_one.dao.RoleDao;
import com.pro_one.dao.UserDao;
import com.pro_one.entity.PageResult;
import com.pro_one.entity.QueryPageBean;
import com.pro_one.pojo.Permission;
import com.pro_one.pojo.Role;
import com.pro_one.pojo.User;
import com.pro_one.service.UserService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private PermissionDao permissionDao;
    @Autowired
    private BCryptPasswordEncoder passwordEncoder;

    /**
     * 根据用户名查询用户信息以及角色和权限信息
     *
     * @param username
     * @return
     */
    @Override
    public User findUserByUsername(String username) {
        User user = userDao.findUserByUsername(username);
        if (user == null) {
            return null;
        }
        Set<Role> roles = roleDao.findRolesByUserId(user.getId());
        if (roles != null && roles.size() > 0) {
            for (Role role : roles) {
                Set<Permission> permissions = permissionDao.findPermissionsByRoleId(role.getId());
                if (permissions != null && permissions.size() > 0) {
                    role.setPermissions(permissions);
                }
            }
            user.setRoles(roles);
        }
        return user;
    }

    @Override
    public PageResult findAllUser(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<User> allUser = userDao.findAllUser(queryPageBean.getQueryString());
        List<User> result = allUser.getResult();
        for (int i = 0; i < result.size(); i++) {
            User user = result.get(i);
            user.setPassword(null);
            if ("admin".equals(user.getUsername())) {
                result.remove(i);
            }
        }
        PageResult pageResult = new PageResult(allUser.getTotal(), result);
        return pageResult;
    }

    @Override
    public void addUser(User user,Integer[] roleIds) {
        String encode = passwordEncoder.encode(user.getPassword());
        user.setPassword(encode);
        userDao.addUser(user);
        Integer userId = user.getId();
        for (Integer roleId : roleIds) {
            Map<String, Integer> map = new HashMap<>();
            map.put("userId",userId);
            map.put("roleId",roleId);
            userDao.addUserIdAndRoleId(map);
        }

    }

    @Override
    public void deleteUser(Integer id) {
        userDao.deleteUserIdAndRoleIdByRoleId(id);
        userDao.deleteUser(id);
    }

    @Override
    public void updateUser(User user,Integer[] roleIds) {
        if ("admin".equals(user.getUsername())) {
            return;
        }
        deleteUser(user.getId());
        addUser(user,roleIds);
    }

    @Override
    public Integer[] findUserIdAndRoleIdByUserId(Integer userId) {
        Integer[] userIdAndRoleIdByUserId = userDao.findUserIdAndRoleIdByUserId(userId);
        return userIdAndRoleIdByUserId;
    }
}
